package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.*
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * 机器周 保养
 */
class MachineWeekCareModel(application: Application) : AndroidViewModel(application) {


    private val postAddMachine: MutableLiveData<String> = MutableLiveData()
    private val getMachineData: MutableLiveData<MutableList<MachineCareWeekBean>> = MutableLiveData()
    private val getMachineCheckList:MutableLiveData<BaseResponse<MacCareWeekList>> = MutableLiveData()
    fun getAddMachineData():LiveData<String>{
        return postAddMachine
    }
    fun getMachineData():LiveData<MutableList<MachineCareWeekBean>>{
        return getMachineData
    }
    fun getMachineCheckListData():LiveData<BaseResponse<MacCareWeekList>>{
        return getMachineCheckList
    }
    /**
     * 新增机器周 保养
     */
    fun addMachineCheck(machineBean: MachineCareWeekBean) {

        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, Gson().toJson(machineBean))
        RetrofitManager.apiService(ApiService::class.java)
            .postAddMachineCareWeekAdd(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postAddMachine.postValue(response?.body()?.message)
                }

            })

    }

    /**
     * 修改周保养
     */
    fun upDateMachineCheck(machineBean: MachineCareWeekBean) {

        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, Gson().toJson(machineBean))
        RetrofitManager.apiService(ApiService::class.java)
            .postUpDateMachineCareWeekUpdate(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postAddMachine.postValue(response?.body()?.message)
                }

            })

    }

    /**
     * 查询
     */
    fun getMachineCheck(machineNo:String?,workDay:String?,empId:String?) {

        RetrofitManager.apiService(ApiService::class.java)
            .getMachineCareWeek(machineNo,workDay,empId)
            .enqueue(object : MesCallBack<BaseResponse<MutableList<MachineCareWeekBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<MutableList<MachineCareWeekBean>>>?,
                    response: Response<BaseResponse<MutableList<MachineCareWeekBean>>>?
                ) {
                    getMachineData.postValue(response?.body()?.result)
                }

            })

    }

    /**
     * 查询List
     */
    fun getMachineCheckList(currentPage:Int?,pageSize:Int?,empId:String?,workDay:String?) {

        RetrofitManager.apiService(ApiService::class.java)
            .getMachineCareWeekList(currentPage,pageSize,empId,workDay)
            .enqueue(object : MesCallBack<BaseResponse<MacCareWeekList>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<MacCareWeekList>>?,
                    response: Response<BaseResponse<MacCareWeekList>>?
                ) {
                    getMachineCheckList.postValue(response?.body())
                }

            })

    }




}